<template>
	<view class="wrap">
		<view class="main">
			<view class="tit">
				导入客户地址
			</view>
			<view class="importbtn" @click="importFile()">
				选择文件
			</view>
			<view class="tips_body">
				<view class="tips_tit">
					温馨提示：
				</view>
				<view class="tips_lit">
					<view class="lit_cell">
						1、请选择.xls、.xlsx格式的文件
					</view>
					<view class="lit_cell">
						2、请根据示例中的模板上传文件否则可能导入失败
					</view>
					<view class="lit_cell">
						3、将文件发送至微信聊天中即可选择相应文件
					</view>
				</view>
				<view class="tips_btn" @click="lookimg">
					查看示例
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import cfg from '@/config/index.config.js';
	export default {
		data() {
			return {
				type:1,//1我的客户 2赠送客户
			};
		},
		onLoad(e) {
			this.type = e.type
		},
		methods:{
			lookimg(){
				uni.previewImage({
					urls:['https://gift.xiaojusmart.com/Data/customer_file_temp.png']
				})
			},
			importFile(){
				wx.chooseMessageFile({
					count:1,
					type:'file',
					extension:['xls', 'xlsx'],
					success: async (res)=>{
						let tempUrl = res.tempFiles[0]
						this.sendImport(tempUrl)
					}
				})
			},
			async sendImport(file){
				const token = uni.getStorageSync('xj_token')
				this.$pv.load('正在上传');
				let url = this.type==1 ? '/Customer/Import' : this.type == 2 ? '/SendGift/Import' : ''
				console.log(url,file);
				uni.uploadFile({
					url: cfg.baseURL+url, //仅为示例，非真实的接口地址
					filePath: file.path,
					name: 'file',
					header:{
						token:token
					},
					success: (res) => {
						let rex = JSON.parse(res.data) 
						console.log(rex);
						if(rex.code==0){
							this.$pv.msg(rex.message)
							uni.$emit('customUpdate',rex.data)
							setTimeout(()=>{
								uni.navigateBack()
							},700)
						}else{
							this.$pv.msg(rex.message)
						}
						this.$pv.closeLoad()
					},
					fail: (err) => {
						console.log(err);
						this.$pv.closeLoad()
						this.$pv.msg(err.errMsg)
					}
				});
					
				
			},
			async uploading(url) {
				this.$pv.load('正在上传');
				const r = await this.$upload(url,()=>{});
				this.$pv.closeLoad()
				console.log('-----------',r);
				return r;
			},
		}
	}
</script>

<style lang="scss">
page{
	background-color: #f6f6f6;
}
.wrap{
	padding: 24rpx;
	.main{
		background: #FFFFFF;
		border-radius: 16rpx;
		padding: 24rpx;		
		.tit{
			font-weight: bold;
			font-size: 32rpx;
			color: #333333;
			margin-bottom: 20rpx;
		}
		.importbtn{
			width: 100%;
			height: 70rpx;
			font-size: 30rpx;
			font-weight: bold;
			color: #fff;
			letter-spacing: 1px;
			display: flex;
			align-items: center;
			justify-content: center;
			border: none;
			border-radius: 44rpx;
			background: linear-gradient(98deg, #FFAD26 0%, #FE4E01 100%);
		}
		.tips_body{
			margin-top: 26rpx;
			.tips_tit{
				font-weight: bold;
				font-size: 28rpx;
				color: #999;
				margin-bottom: 10rpx;
			}
			.tips_lit{
				
				view{
					font-size: 26rpx;
					color: #999;
				}
			}
			.tips_btn{
				margin-top: 10rpx;
				font-size: 28rpx;
				color: #55aaff;
				text-decoration: underline;
			}
		}
	}
}
</style>
